package com.basic.technology.study.introduction.thread.keyword;

import java.util.concurrent.TimeUnit;

public class VolatileSimple {

    boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        VolatileSimple volatileSimple = new VolatileSimple();
        volatileSimple.aaa1();
    }

    public void aaa() throws InterruptedException {
        Thread t = new Thread(() -> {
            while(flag){
                System.out.println("aaa");
            }
        });
        t.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("end:"+System.currentTimeMillis());
        flag = false;
        System.out.println("22222222");
    }


    public synchronized void aaa1() throws InterruptedException {
        Thread t = new Thread(() -> {
            while (flag) {
                System.out.println("aaa");
            }
        });
        t.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("end:" + System.currentTimeMillis());
        flag = false;
        System.out.println("22222222");
    }

}
